﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CustomControlz.Validations;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.DXErrorProvider;
using Utilities;

namespace CustomControlz.XtraEditors.TextEdits
{
    public partial class ccTextEdit : TextEdit
    {
        private bool _isEditable = true;
        private EValueType _valueType = EValueType.Text;
        private ClsCommonFn _oCommFn = new ClsCommonFn();
        private EtextEditType _textEditType = EtextEditType.Normal;
        public enum EvalidationType
        {
            IsNullOrEmpty = 0,
            MatchPasswords = 1
        }
        public enum EValueType
        {
            Text = 0,
            TextDecimal = 1,
            Int = 2,
            Decimal = 3
        }

        public enum EtextEditType
        {
            Normal = 0,
            CodeSeries = 1,
            Numeric = 2,
            Disabled = 4
        }
        public ccTextEdit()
        {
            InitializeComponent();
            if (ProvideValidation && ValidationProvider != null)
            {
                ValidationProvider.SetValidationRule(this, new IsNullOrEmpty()
                {
                    ErrorText = string.Format("Please Enter {0}", Text),
                    ErrorType = ErrorType.Warning
                });
            }
        }


        void ccTextEdit_HandleCreated(object sender, EventArgs e)
        {
            if (ProvideValidation && ValidationProvider != null)
            {
                ValidationProvider.SetValidationRule(this, new IsNullOrEmpty()
                {
                    ErrorText = string.Format("Please Enter {0}", Text),
                    ErrorType = ErrorType.Warning
                });
            }
        }
        private void ccTextEdit_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (_valueType != EValueType.Text)
                _oCommFn.DecimalTb(this, e);
        }


        public override sealed string Text
        {
            get { return base.Text; }
            set { base.Text = value; }
        }
        public bool ProvideValidation { get; set; }
        public ccDxValidationProvider ValidationProvider { get; set; }
        public bool IsEditable
        {
            get { return _isEditable; }
            set { _isEditable = value; }
        }
        public EValueType ValueType
        {
            get { return _valueType; }
            set
            {
                _valueType = value;
                if (value == EValueType.Int || value == EValueType.TextDecimal)
                    Tag = "int";
                else if (value == EValueType.Decimal)
                    Tag = "decT";
            }
        }
        public decimal? ValueDec
        {
            get
            {
                decimal x = 0;
                Decimal.TryParse(Text, out x);
                return x;
            }
            set
            {
                if (_valueType == EValueType.Decimal)
                    Text = value != null ? value.ToString() : "0";
            }
        }

        public EtextEditType TextEditType
        {
            get { return _textEditType; }
            set
            {
                _textEditType = value;
                if (value == EtextEditType.CodeSeries || value == EtextEditType.Numeric)
                {
                    this.Properties.AppearanceDisabled.BackColor = Color.Aqua;
                    this.Properties.AppearanceDisabled.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Bold);
                    this.Properties.AppearanceDisabled.ForeColor = Color.FromArgb(200, 25, 25);
                    this.Properties.AppearanceDisabled.Options.UseBackColor = true;
                    this.Properties.AppearanceDisabled.Options.UseFont = true;
                    this.Properties.AppearanceDisabled.Options.UseForeColor = true;
                    this.Properties.AppearanceDisabled.TextOptions.HAlignment = HorzAlignment.Center;
                    Properties.Enabled = false;
                }
                else
                {
                    Properties.ReadOnly = false;
                }
            }
        }
    }
}
